package com.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.AuthCodeDao;
import com.enetiy.Authcode;
import com.enetiy.Authcodeinfo;

public class AuthCodeDapImpl extends HibernateDaoSupport implements AuthCodeDao {
	public Serializable insertAuthCode(Authcode auth) throws Exception {
		Serializable save = this.getHibernateTemplate().save(auth);
		return save;
	}

	@Override
	public void deleteAuthCode(Authcode auth) throws Exception {
		this.getHibernateTemplate().delete(auth);
	}

	@Override
	public boolean selectAuthCode(String phone,String code) throws Exception {
		Query query = this.getSession().createQuery(" FROM Authcode WHERE phone = ? and code = ?");
		query.setParameter(0, phone);
		query.setParameter(1, code);
		List<Authcode> list = query.list();
		return !list.isEmpty();
	}

	@Override
	public Authcode selectAuthCode(String phone) throws Exception {
		Query query = this.getSession().createQuery(" FROM Authcode WHERE phone = ?");
		query.setParameter(0, phone);
		List <Authcode>list = query.list();
		return list.isEmpty()?null:list.get(0);
	}

	@Override
	public void insertAuthCodeInfo(Authcodeinfo authcodeinfo) throws Exception {
		this.getHibernateTemplate().save(authcodeinfo);
	}
}
